package cn.wolfcode.luowowo2.mgrsite.web.controller;

import cn.wolfcode.luowowo2.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo2.article.domain.StrategyDetail;
import cn.wolfcode.luowowo2.article.service.*;
import cn.wolfcode.luowowo2.common.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo2.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Albert on 2019/7/9.
 */
@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;


    @RequestMapping("list")
    public String list(Model model, StrategyDetailQueryObject qo){
        PageInfo<StrategyDetail> pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyDetail/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){

        model.addAttribute("strategies",strategyService.listAll());
        model.addAttribute("themes",strategyThemeService.listAll());
        if (id==null) {
            return "strategyDetail/input";
        }

        StrategyDetail strategyDetail = strategyDetailService.get(id);
        List<String> tagsArr = strategyTagService.getByDetailId(id);
        if (tagsArr.size()!=0) {
            StringBuilder tags = new StringBuilder(80);
            for (String tag : tagsArr) {
                tags.append(tag).append(",");
            }
            model.addAttribute("tags",tags.toString().substring(0,tags.length()-1));
        }

        model.addAttribute("strategyDetail",strategyDetail);
        return "strategyDetail/input";
    }

    @RequestMapping("getCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId){
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(StrategyDetail strategyDetail,String[] tags){
        strategyDetailService.saveOrUpdate(strategyDetail,tags);

        return new AjaxResult();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public AjaxResult changeState(Byte state,Long id){
        strategyDetailService.updateStateById(state,id);

        return new AjaxResult();
    }

}
